package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @outhor
 * @date 2020/2/8 16:03
 */

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User userByUserName(String username) {
        return userMapper.userByUserName(username);
    }

    @Override
    public void changePassword(String pass, String username) {
        userMapper.changePassword(pass,username);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<User> userList = userMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), userList);
    }

    @Override
    public void add(User user) {
        //密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePassword = encoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        //插入基本信息(返回主键)-自增主键回填
        userMapper.add(user);
        Integer userId = user.getId();
        List<Integer> roleIds = user.getRoleIds();
        if (CollectionUtil.isNotEmpty(roleIds)) {
            List<Map> userAndrole = new ArrayList<>();
            for (Integer roleId : roleIds) {
                Map map = new HashMap<>();
                map.put("userId", userId);
                map.put("roleId", roleId);
                userAndrole.add(map);
            }
            userMapper.addUserAndRole(userAndrole);
        }
    }

    @Override
    public Map findByid(Integer id) {
        //通过id查询基本信息
        User user = userMapper.findByid(id);

        //通过ID查询中间表中的role_id
        List<Integer> roleIds =userMapper.findUserAndRole(id);

        //查询所有role信息
        Set<Role> roles = userMapper.roleFindAll();

        //用MAP封装数据并返回
        Map map = new HashMap<>();
        map.put("user",user);
        map.put("roleIds",roleIds);
        map.put("roles",roles);
        return map;
    }

    @Override
    public void updata(User user) {
        Integer userId = user.getId();
        List<Integer> roleIds = user.getRoleIds();
        //更新user基本信息
        userMapper.updata(user);
        //删除t_user_role表中userId的数据
        userMapper.deleteUserAndRoleById(userId);
        //添加t_user_role表中userId的数据
        if (CollectionUtil.isNotEmpty(roleIds)) {
            List<Map> userAndrole = new ArrayList<>();
            for (Integer roleId : roleIds) {
                Map map = new HashMap<>();
                map.put("userId", userId);
                map.put("roleId", roleId);
                userAndrole.add(map);
            }
            userMapper.addUserAndRole(userAndrole);
        }
    }

    @Override
    public Set<Role> roleFindAll() {
        return userMapper.roleFindAll();
    }

    @Override
    public void delete(Integer id) {
        userMapper.deleteUserAndRoleById(id);
        userMapper.delete(id);
    }

    @Override
    public Integer findIdByUsername(String username) {

        return userMapper.findIdByUsername(username);
    }
}
